package com.antler.smt.portal.service.impl;

import com.antler.smt.constant.OrderConstant;
import com.antler.smt.entity.OrderEntity;
import com.antler.smt.entity.RefundRecordEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.OrderEntityMapper;
import com.antler.smt.portal.mapper.OrderInfoMapper;
import com.antler.smt.portal.mapper.RefundRecordEntityMapper;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.RefundService;
import com.antler.smt.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhangyi
 * @date 2018/6/7 11:59
 */
@Service
public class RefundServiceImpl implements RefundService {

    @Autowired
    private OrderEntityMapper orderEntityMapper;

    @Autowired
    private RefundRecordEntityMapper refundRecordEntityMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    /**
     * 申请退款实现
     *
     * @param orderId
     * @return
     */
    @Override
    public Result applyRefund(Long orderId) {
        Result result = null;

        OrderEntity orderEntity = orderInfoMapper.selectByPrimaryKey(orderId);
        RefundRecordEntity refundRecordEntity = new RefundRecordEntity();
        OrderEntity orderEntity1 = new OrderEntity();
        orderEntity1.setId(orderId);
        orderEntity1.setStatus(OrderConstant.ORDER_APPLY_REFUND);
        int i1 = orderEntityMapper.updateByPrimaryKeySelective(orderEntity1);
        //添加退款申请时间
        refundRecordEntity.setApplyRefundTime(DateUtil.stampToDate(System.currentTimeMillis()));
        //添加退款订单id
        refundRecordEntity.setOrderId(orderId);
        //保存退款前状态
        refundRecordEntity.setRefundStatus(orderEntity.getStatus());
        int i = refundRecordEntityMapper.insertSelective(refundRecordEntity);
        result = ((i == 0 && i1 == 0)? new Result(ResultCodeEnum.APPEND_DATA_ERROR):new Result(ResultCodeEnum.OPERATION_SUCCESS));

        return result;
    }
}
